package com.china08.yxyapi.api.home.activity.declaration;

import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.huodong.declaration.YxyHuodongHrbDeclaration;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.declaration.YxyHuodongHrbDeclarationRepository;

/**
 * 
 * 哈尔滨教师宣言 -- 我的宣言
 *
 */
@CrossOrigin
@RestController
public class ActivityHrbMyDeclarationController extends YxyApiBaseController {

    @Autowired
    private YxyHuodongHrbDeclarationRepository yxyHuodongHrbDeclarationRepository;

    @RequestMapping(value = "/api_yw/home/activity/hrb/my/declaration/sch", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) ReqModel reqModel) {

        String userId = getProfile(request, response).getId();
        String schoolId = getProfile(request, response).getSchoolId();
        Integer loginStatus = 0;
        RespModel model = new RespModel();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd   hh:mm");
        // 判断是否登录
        if (null == userId || !(userId.length() > 0)) {
            loginStatus = 0;
        } else {
            loginStatus = 1;
        }
        model.loginStatus = loginStatus;
        if (loginStatus == 1 && userId != null && schoolId != "") {
            YxyHuodongHrbDeclaration yDeclaration = yxyHuodongHrbDeclarationRepository.findByTeacherId(userId);
            if (null != yDeclaration) {
                model.myDeclaration = yDeclaration.getDeclaration();
                model.myDate = dateFormat.format(yDeclaration.getLastModifiedDate());
            } else {
                model.myDeclaration = "";
            }
        }
        return model;
    }

    protected static class ReqModel extends YxyApiReqModel {

    }

    protected static class RespModel extends YxyApiRespModel {
        // 0 未登录 1 登录
        public Integer loginStatus;
        // 我的宣言
        public String myDeclaration;
        public String myDate;

    }

}
